//
//  AppDelegate.swift
//  0623微博(swift)
//
//  Created by 岳挺 on 15/6/23.
//  Copyright © 2015年 yueting. All rights reserved.
//

import UIKit
import AFNetworking

//定义一个全局的用户账户变量，优化性能可以防止重复加载
var sharedUserAccount = UserAccount.loadUserAccount()
let notification = "switchToMainViewController"

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {

    var window: UIWindow?


    func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
        // Override point for customization after application launch.
        
        window = UIWindow(frame: UIScreen.mainScreen().bounds)
        window?.backgroundColor = UIColor.whiteColor()
        window?.rootViewController = MainViewController()
//        window?.rootViewController = WelcomeViewController()
        
        //设置全局外观代理
        setAppearProperty()
        
        //设置网络缓存和指示器
        setNetWorking()
        
        //切换根控制器
//        window?.rootViewController = switchRootViewController()
        
        //注册通知
        NSNotificationCenter.defaultCenter().addObserver(self, selector: "switchToMainViewController:", name: notification, object: nil)
        
        window?.makeKeyAndVisible()

        print(sharedUserAccount)
        
        return true
    }
    
    ///切换根控制器
    private func switchRootViewController () ->UIViewController{
        
        //如果用户登录，检查是否有版本更新，如果有更新切换到新特性，如果没有更新切换到欢迎界面
        //如果用户未登录直接到main控制器
        if sharedUserAccount != nil {
            return checkCurrentVersionIsUpdate() ? NewFeatureViewController() : WelcomeViewController()
        }
            return MainViewController()
    }
    
    ///监听到通知，切换到主控制器
    func switchToMainViewController (n : NSNotification){
        let isMainVc = n.object as! Bool
        window?.rootViewController = isMainVc == true ? MainViewController() : WelcomeViewController()
    }
    
    ///检查版本是否更新
    private func checkCurrentVersionIsUpdate () -> Bool{
        //获取当前版本信息
        
        let versionKey = "versionKey"
        let v = NSBundle.mainBundle().infoDictionary!["Bundle versions string, short"] as! String
        let version = Double(v)
        //["CFBundleShortVersionString"]
        print(version)
        //获取上一个版本信息
        let lastVersion = NSUserDefaults.standardUserDefaults().doubleForKey(versionKey)
        
        //比较版本是否相同
        if version == lastVersion {
            return false
        }
        
        //将当前版本存入偏好设置
        NSUserDefaults.standardUserDefaults().setDouble(version!, forKey: versionKey)
        return true
    }
    
    func setNetWorking (){
        //设置网络状态指示器
        AFNetworkActivityIndicatorManager.sharedManager().enabled = true
        //设置网络缓存
        let urlCache = NSURLCache(memoryCapacity: 4 * 1024 * 1024, diskCapacity: 20 * 1024 * 1024, diskPath: nil)
        NSURLCache.setSharedURLCache(urlCache)
        
    }
    
    func setAppearProperty (){
        UINavigationBar.appearance().tintColor = UIColor.orangeColor()
        UITabBar.appearance().tintColor = UIColor.orangeColor()
    }
}

